

/////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////
// 00 FOLDER SETTINGS

** Definition of Start Point
* set path
global 		path 	""


** Input data folder
global bhc_in 		"$path/0 Data/BHCY9C/Input"
global crsp_in		"$path/0 Data/CRSP"
global fama_in		"$path/0 Data/FamaFrench"

** Output data folder 
global bhc_out 		"$path/0 Data/BHCY9C/Output"

** Processed data folder 
global processed 	"$path/0 Data/_Processed"

/////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////
// 01 Load and Combine Risk factors (daily)


** (a) Load FF3 (daily)
*****************************
{
import 		delimited using "$fama_in/F-F_Research_Data_Factors_daily.csv", clear varnames(4) rowrange(4) delimiter(",")

drop 		if mktrf == .

** Format date
gen 		year 	= substr(v1,1,4)
gen 		month	= substr(v1,5,2)
gen 		day 	= substr(v1,7,2)

destring 	year month day, replace
gen 		date 	= mdy(month, day, year)
format 		date	%td

drop 		v1 year month day
order	 	date

** Adjust Variable Format
foreach var of varlist mktrf smb hml rf {
    replace `var' = `var' / 100
	}


** Save
compress
save "$processed/FF3_Factors_jul1926_october2022_daily", replace
}


** Note: updated 19 March 2023


** (b) Load Carhart Momentum (daily)
*****************************
{
import delimited using "$fama_in/F-F_Momentum_Factor_daily.csv", clear  varnames(14) rowrange(14) delimiter(",")

drop 		if mom == .

** Format date
gen 		year 	= substr(v1,1,4)
gen 		month	= substr(v1,5,2)
gen 		day 	= substr(v1,7,2)

destring 	year month day, replace
gen 		date 	= mdy(month, day, year)
format 		date	%td

drop 		v1 year month day
order	 	date

** Adjust Variable Format
foreach var of varlist mom {
    replace `var' = `var' / 100
	}

** Save
compress
save "$processed/FF_Momentum_nov1926_october2022_daily", replace
}

** Note: updated 19 March 2023


** (c) Load FF5 (daily)
*****************************
{
import delimited using "$fama_in/F-F_Research_Data_5_Factors_2x3_daily.csv", clear  varnames(4) rowrange(4) delimiter(",")

drop 		if mktrf == .

** Format date
tostring 	v1, replace usedisplayformat
gen 		year 	= substr(v1,1,4)
gen 		month	= substr(v1,5,2)
gen 		day 	= substr(v1,7,2)

destring 	year month day, replace
gen 		date 	= mdy(month, day, year)
format 		date	%td

drop 		v1 year month day
order	 	date

** Adjust Variable Format
foreach var of varlist mktrf smb hml cma rmw rf {
    replace `var' = `var' / 100
	}

** Save
compress
save "$processed/FF5_Factors_aug1963_october2022_daily", replace
}


** Note: updated 19 March 2023


** (d) Load US Government Bond Index (daily)
*****************************

** These data are not from FF website, but from S&P (https://www.spglobal.com/spdji/en/indices/fixed-income/sp-us-treasury-bond-current-10-year-index/#overview)
** Login with own credentials (s.steffen@fs.de) and download (1) performance and (2) yield to maturity
** Only download for 10 years back possible, i.e. update of existing file


{
import excel using "$fama_in/US_Bond_Index.xls", firstrow cellrange(A7:E3195) clear 

gen 	date = Effectivedate
format 	date %td

rename 	LogReturn 		gov_bond_log_return
rename  SimpleReturn	gov_bond_return

keep	date gov_bond_log_return gov_bond_return

** Save
compress
save "$processed/US_Bond_Index_Return_daily (v 2022)", replace
}


** Note: updated 19 March 2023


** (e) Load US IG Grade Bond Index (daily)
*****************************

** These data are not from FF website, but from S&P (https://www.spglobal.com/spdji/en/indices/fixed-income/dow-jones-equal-weight-us-issued-corporate-bond-index/#overview)
** Login with own credentials (s.steffen@fs.de) and download (1) performance and (2) yield to maturity
** Only download for 10 years back possible, i.e. update of existing file

{
import excel using "$fama_in/IG_Bond_Index.xls", firstrow cellrange(A7:E3418) clear 

gen 	date = Effectivedate
format 	date %td

rename 	LogReturn 		ig_bond_log_return
rename  SimpleReturn	ig_bond_return

keep	date ig_bond_log_return ig_bond_return

** Save
compress
save "$processed/IG_Bond_Index_Return_daily (v 2022)", replace
}
** Note: updated 12 December 2022


/////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////
// 02 Comine Risk factors to one file (daily)

{
** Load FF3 Factors dataset
use 	"$processed/FF3_Factors_jul1926_october2022_daily",  clear

* Rename SMB as it is computed differently in FF3 vs FF5 (6 vs 9 portfolios)
rename smb smb_ff3



** Merge with Momentum Factor
merge 	1:1 date using "$processed/FF_Momentum_nov1926_october2022_daily"
drop	if _merge == 2
drop	_merge

** Merger with FF5 Factors
merge 	1:1 date using "$processed/FF5_Factors_aug1963_october2022_daily", update
drop	if _merge == 2
drop	_merge

* Rename SMB as it is computed differently in FF3 vs FF5 (6 vs 9 portfolios)
rename smb smb_ff5

** Merge with US Gov. Bond Returns
merge 	1:1 date using "$processed/US_Bond_Index_Return_daily (v 2022)"
drop	if _merge == 2
drop	_merge

** Merge with US IG Bond Returns
merge 	1:1 date using "$processed/IG_Bond_Index_Return_daily (v 2022)"
drop	if _merge == 2
drop	_merge


label var gov_bond_log_return 	"US Gov. Bond"
label var gov_bond_return 		"US Gov. Bond"
label var ig_bond_log_return	"US IG Bond"
label var ig_bond_return		"US IG Bond"


** Save
compress 
save "$processed/FF_Risk_Factors_Daily (v 2022)", replace
}

** Note: updated 19 March 2023

////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////
// 03 Load and Combine Risk factors (monthly)


** (a) Load FF3 (monthly)
*****************************
{
import 		delimited using "$fama_in/F-F_Research_Data_Factors.csv", clear varnames(4) rowrange(4:1160) delimiter(",")

drop 		if mktrf == .

** Format date
tostring 	v1 		, gen(date)
gen 		year 	= substr(date,1,4)
gen 		month	= substr(date,5,2)
drop		date

destring	year month, replace
gen			day_v2		= 1
gen			month_v2 	= month + 1 if month <= 11
gen 		year_v2	= year 		if month <= 11
replace 	month_v2 	= 1			if month == 12
replace 	year_v2		= year + 1  if month == 12

gen 		date_v2 	= mdy(month_v2, day_v2, year_v2)
format 		date_v2	%td

replace		date_v2 = date_v2 - 1
gen			date = mofd(date_v2)
format 		date %tm

drop 		v1 year* month* day
rename 		date_v2 date_daily
order	 	date

** Adjust Variable Format
foreach var of varlist mktrf smb hml rf {
    replace `var' = `var' / 100
	}



** Save
compress
save "$processed/FF3_Factors_jul1926_october2022_monthly", replace
}

** Note: updated 19 March 2023



** (b) Load Carhart Momentum (monthly)
*****************************
{
import delimited using "$fama_in/F-F_Momentum_Factor.csv", clear  varnames(14) rowrange(14:1164) delimiter(",")

drop 		if mom == .

** Format date
tostring 	v1 		, gen(date)
gen 		year 	= substr(date,1,4)
gen 		month	= substr(date,5,2)
drop		date

destring	year month, replace
gen			day_v2		= 1
gen			month_v2 	= month + 1 if month <= 11
gen 		year_v2	= year 		if month <= 11
replace 	month_v2 	= 1			if month == 12
replace 	year_v2		= year + 1  if month == 12

gen 		date_v2 	= mdy(month_v2, day_v2, year_v2)
format 		date_v2	%td

replace		date_v2 = date_v2 - 1
gen			date = mofd(date_v2)
format 		date %tm

drop 		v1 year* month* day
rename 		date_v2 date_daily
order	 	date


** Adjust Variable Format
foreach var of varlist mom {
    replace `var' = `var' / 100
	}

** Save
compress
save "$processed/FF_Momentum_jan1927_october2022_monthly", replace
}

** Note: updated 19 March 2023


** (c) Load FF5 (monthly)
*****************************
{
import delimited using "$fama_in/F-F_Research_Data_5_Factors_2x3.csv", clear  varnames(4) rowrange(4:716) delimiter(",")

drop 		if mktrf == .

** Format date
tostring 	v1 		, gen(date)
gen 		year 	= substr(date,1,4)
gen 		month	= substr(date,5,2)
drop		date

destring	year month, replace
gen			day_v2		= 1
gen			month_v2 	= month + 1 if month <= 11
gen 		year_v2	= year 		if month <= 11
replace 	month_v2 	= 1			if month == 12
replace 	year_v2		= year + 1  if month == 12

gen 		date_v2 	= mdy(month_v2, day_v2, year_v2)
format 		date_v2	%td

replace		date_v2 = date_v2 - 1
gen			date = mofd(date_v2)
format 		date %tm

drop 		v1 year* month* day
rename 		date_v2 date_daily
order	 	date


** Adjust Variable Format
foreach var of varlist mktrf smb hml cma rmw rf {
    replace `var' = `var' / 100
	}

** Save
compress
save "$processed/FF5_Factors_jul1963_october2022_monthly", replace
}


** Note: updated 19 March 2023


** (d) Load US Government Bond Index (monthly)
*****************************
{
import excel using "$fama_in/US_Bond_Index.xls", firstrow cellrange(A7:E3129) clear 

gen 	date = mofd(Effectivedate)
format 	date %tm

* Compute Monthly Returns
* (i) With log returns (= sum)
bys date: egen gov_bond_log_return = total(LogReturn), missing


* (ii) with simple returns 
bys date: egen max = max(Effectivedate)
bys date: egen min = min(Effectivedate)

keep 	if Effectivedate == max 

gen		gov_bond_return = (SPUSTreasuryBondCurrent1 - SPUSTreasuryBondCurrent1[_n-1]) / SPUSTreasuryBondCurrent1[_n-1]

rename 	Effectivedate date_daily
format	date_daily %td

keep	date date_daily gov_bond_log_return gov_bond_retur

** Save
compress
save "$processed/US_Bond_Index_Return_monthly (v 2022)", replace
}

** Note: updated 19 March 2023

** (e) Load US IG Grade Bond Index (monthly)
*****************************
{
import excel using "$fama_in/IG_Bond_Index.xls", firstrow cellrange(A7:E3352) clear 


gen 	date = mofd(Effectivedate)
format 	date %tm

* Compute Monthly Returns
* (i) With log returns (= sum)
bys date: egen ig_bond_log_return = total(LogReturn), missing

* (ii) with simple returns 
bys date: egen max = max(Effectivedate)
bys date: egen min = min(Effectivedate)

keep 	if Effectivedate == max 

gen		ig_bond_return = (DowJonesEqualWeightUSIssu - DowJonesEqualWeightUSIssu[_n-1]) / DowJonesEqualWeightUSIssu[_n-1]

rename 	Effectivedate date_daily
format	date_daily %td

keep	date date_daily ig_bond_log_return ig_bond_return

** Save
compress
save "$processed/IG_Bond_Index_Return_monthly (v 2022)", replace
}


** Note: updated 19 March 2023


** (f) Load Gandhi et al. Size Factor (monthly)
*****************************************************

** Size factor updated from Gandhi website (https://www.priyankgandhi.net/data-and-code-1)
** Only update until 12 / 2019

{
import excel using "$fama_in/size_factor.xlsx", firstrow  clear 

rename A year
rename B month
rename C size_factor

** Format date
gen			day_v2		= 1
gen			month_v2 	= month + 1 if month <= 11
gen 		year_v2		= year 		if month <= 11
replace 	month_v2 	= 1			if month == 12
replace 	year_v2		= year + 1  if month == 12

gen 		date_v2 	= mdy(month_v2, day_v2, year_v2)
format 		date_v2	%td

replace		date_v2 = date_v2 - 1
gen			date = mofd(date_v2)
format 		date %tm

drop 		year* month* day
rename 		date_v2 date_daily
order	 	date


** Save
compress
save "$processed/Gandhi_SizeFactor_monthly", replace
}



** (g) Load 49 Industry Portfolios (monthly)
*****************************
import delimited using "$fama_in/49_Industry_Portfolios.CSV", clear  varnames(12) rowrange(12:1168) delimiter(",")

foreach var of varlist agric-other{
	replace `var' = . if (`var' < -99)
 	}


** Format date
tostring 	v1 		, gen(date)
gen 		year 	= substr(date,1,4)
gen 		month	= substr(date,5,2)
drop		date

destring	year month, replace
gen			day_v2		= 1
gen			month_v2 	= month + 1 if month <= 11
gen 		year_v2	= year 		if month <= 11
replace 	month_v2 	= 1			if month == 12
replace 	year_v2		= year + 1  if month == 12

gen 		date_v2 	= mdy(month_v2, day_v2, year_v2)
format 		date_v2	%td

replace		date_v2 = date_v2 - 1
gen			date = mofd(date_v2)
format 		date %tm

drop 		v1 year* month* day
rename 		date_v2 date_daily
order	 	date


** Adjust Variable Format
foreach var of varlist agric-other {
    replace `var' = `var' / 100
	}

** Save
compress
save "$processed/49_Industry_Portfolios_monthly (v 2022)", replace



** Note: updated 19 March 2023


/////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////
// 04 Comine Risk factors to one file

{
** Load FF3 Factors dataset
use 	"$processed/FF3_Factors_jul1926_october2022_monthly",  clear

* Rename SMB as it is computed differently in FF3 vs FF5 (6 vs 9 portfolios)
rename smb smb_ff3


** Merge with Momentum Factor
merge 	1:1 date using "$processed/FF_Momentum_jan1927_october2022_monthly"
drop	if _merge == 2
drop	_merge

merge 	1:1 date using "$processed/FF5_Factors_jul1963_october2022_monthly", update
** Merger with FF5 Factors
drop	if _merge == 2
drop	_merge

* Rename SMB as it is computed differently in FF3 vs FF5 (6 vs 9 portfolios)
rename smb smb_ff5

** Merge with US Gov. Bond Returns
merge 	1:1 date using "$processed/US_Bond_Index_Return_monthly (v 2022)"
drop	if _merge == 2
drop	_merge

** Merge with US IG Bond Returns
merge 	1:1 date using "$processed/IG_Bond_Index_Return_monthly (v 2022)"
drop	if _merge == 2
drop	_merge


** Merge with Gandhi et al. Size Factor
merge 	1:1 date using "$processed/Gandhi_SizeFactor_monthly"
drop	if _merge == 2
drop	_merge

** Merge with 49 Portfolio Returns
merge 	1:1 date using "$processed/49_Industry_Portfolios_monthly (v 2022)"
drop	if _merge == 2
drop	_merge


label var size_factor			"Size Factor"
label var gov_bond_log_return 	"US Gov. Bond"
label var gov_bond_return 		"US Gov. Bond"
label var ig_bond_log_return	"US IG Bond"
label var ig_bond_return		"US IG Bond"


** Save
compress 
save "$processed/FF_Risk_Factors_monthly (v 2022)", replace
}


** Note: updated 19 March 2023
















